package StudyCode.summary.regularexpressionsmatch;

import java.util.regex.Pattern;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class EscapingSpecialCharactersInAPattern {
	public static void main(String[] argv) {
		String patternStr = "i.e.";

		boolean matchFound = Pattern.matches(patternStr, "i.e.");// true
		matchFound = Pattern.matches(patternStr, "ibex"); // true
		System.out.println("匹配结果:" + matchFound);
		matchFound = Pattern.matches("\\Q" + patternStr + "\\E", "i.e."); // true
		System.out.println("匹配结果:" + matchFound);
		matchFound = Pattern.matches("\\Q" + patternStr + "\\E", "ibex"); // false
		System.out.println("匹配结果:" + matchFound);
		patternStr = escapeRE(patternStr); // i\.e\.

		matchFound = Pattern.matches(patternStr, "i.e."); // true
		System.out.println("匹配结果:" + matchFound);
		matchFound = Pattern.matches(patternStr, "ibex"); // false
		System.out.println("匹配结果:" + matchFound);
	}

	public static String escapeRE(String str) {
		Pattern escaper = Pattern.compile("([^a-zA-z0-9])");
		return escaper.matcher(str).replaceAll("\\\\$1");
	}
}
